<template>
  <div class="tobBtoC">
    <div class="contentTopTitle">
      <div class="fl">
        <div class="title">完成情况</div>
      </div>
    </div>
    <div>
      <el-form :inline="true" :model="form" label-width="70px" class="formList">
        <el-form-item label="预算年度">
          <el-date-picker v-model="form.year" type="month" placeholder="选择年" value-format="yyyyMM"></el-date-picker>
        </el-form-item>
        <el-form-item label="区域">
          <dx-cascader :area_id.sync="form.areaId" ref="cascader" />
        </el-form-item>
        <el-form-item class="btnBox">
          <div>
            <el-button size="small" type="primary" @click="search">查询</el-button>
          </div>
          <div>
            <el-button size="small" style="margin-left: 10px" type="orange" @click="exportModal">导出</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-content clearfix">
      <div class="content">
        <span class="fileName">{{fileName}}</span>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="本地网展示" name="first">
            <tab1 :rowData="tableData" :rawTime="time" ref="tab1"></tab1>
          </el-tab-pane>
          <el-tab-pane label="县分展示" name="second">
            <tab2 :rowData="tableData" :rawTime="time" ref="tab2"></tab2>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
import { getSessionStorage } from "@/common/js/storage";
import { $budgetAPI } from "@/api";
import tab1 from "./basic-tab1";
import tab2 from "./basic-tab2";
export default {
  name: "incomeBasic",
  components: {
    tab1,
    tab2,
  },
  data() {
    return {
      form: {
        year: "",
        areaId: "",
      },
      activeName: "first",
      fileName: "",
      flag: 0,
      accountInfo: "",
      tableData: [],
      colDefList: [],
      time: {},
    };
  },
  computed: {
    params() {
      let obj = {
        token: getCookie("token"),
        areaId: this.form.areaId,
        year: this.form.year,
      };
      return obj;
    },
    areaSelect() {
      return this.form.areaSelect;
    },
  },
  created() {
    // if (this.$route.query.row) {
    //   let row = this.$route.query.row;
    //   this.form.areaId = row.areaId;
    //   this.form.year = row.accMonth;
    //   this.resourceId = true;
    // } else {
    //   this.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
    //   this.form.areaId = this.accountInfo.areaId;
    //   this.form.year = this.moment().format("YYYYMM");
    // }
  },
  mounted() {
    let that = this;

    that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
    that.form.areaId = that.accountInfo.areaId;
    that.form.year = that.moment().format("YYYYMM");

    this.init();
    //   $budgetAPI.toBtoCmonth(
    //     {},
    //     (res) => {
    //       if (res || res.code == 0) {
    //         this.form.year = res.content;
    //         this.init();
    //       }
    //     },
    //     (err) => {}
    //   );
  },
  methods: {
    //查询
    search() {
      let params = {
        month: this.form.year,
        areaId: this.form.areaId ? this.form.areaId : this.accountInfo.areaId,
        type: this.flag,
      };
      this.listResourceData(params);
    },
    //导出
    exportModal() {
      let areaId = this.form.areaId;
      let year = this.form.year;
      let token = getCookie("token");
      let type = this.flag;

      if (process.env.NODE_ENV == "development") {
        window.location.href =
          prefixAPI.loc +
          "/api/drawPortrait/exportDrawComplete?token=" +
          token +
          "&areaId=" +
          areaId +
          "&month=" +
          year +
          "&type=" +
          type;
      } else if (process.env.NODE_ENV == "production") {
        window.location.href =
          prefixAPI.hp +
          "/api/drawPortrait/exportDrawComplete?token=" +
          token +
          "&areaId=" +
          areaId +
          "&month=" +
          year +
          "&type=" +
          type;
      }
    },
    listResourceData(params) {
      let that = this;

      $budgetAPI.incomeBasic(
        params,
        (res) => {
          console.log(res);
          if (res.code == 0) {
            that.tableData = res.content;
            that.time.year = params.month.substr(0, 4);
            that.time.month = params.month.substr(4, 2);
            switch (params.type) {
              case 0:
                that.$refs.tab1.init();
              // that.$refs.tab1.tableLoading = false;
              case 1:
                that.$refs.tab2.init();
              // that.$refs.tab2.tableLoading = false;
            }
          } else {
            that.alertError(res);
          }
        },
        (err) => {}
      );
    },
    init() {
      let that = this,
        params = {
          month: this.form.year,
          areaId: this.form.areaId ? this.form.areaId : this.accountInfo.areaId,
          type: 0,
        };
      this.listResourceData(params);
    },
    //数据点击切换页请求不同的数据
    handleClick(tab, event) {
      let params = {
        month: this.form.year,
        areaId: this.form.areaId ? this.form.areaId : this.accountInfo.areaId,
        type: "",
      };
      if (tab.name == "first") {
        params.type = 0;
        this.flag = 0;
        this.listResourceData(params);
      }
      if (tab.name == "second") {
        params.type = 1;
        this.flag = 1;
        this.listResourceData(params);
      }
      if (tab.name == "third") {
        params.flag = 3;
        this.flag = 3;
        this.listResourceData(params);
      }
    },
  },
};
</script>
<style lang="scss">
.tobBtoC {
  .formList {
    .el-form-item.btnBox {
      margin-bottom: 0px;
      width: 24%;
      margin-right: 0;
      .el-form-item__content {
        display: flex;
        width: 100%;
      }
    }
  }
  .upload-demo {
    display: inline-block;
    margin: 0 15px;
  }
}
</style>
